package com.zhangyu.shadow.ec.main.sort.content;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyu
 * @date 2017/11/9
 */

public class SectionDataConverter {

    public final List<SectionBean> convert(String json) {
        final List<SectionBean> beanList = new ArrayList<>();
        final JSONArray jsonArray = JSON.parseObject(json).getJSONArray("data");
        int size = jsonArray.size();
        for (int i = 0; i < size; i++) {
            final JSONObject jsonObject = jsonArray.getJSONObject(i);
            final int sectionId = jsonObject.getInteger("id");
            final String sectionName = jsonObject.getString("section");

            SectionBean sectionBean = new SectionBean(true, sectionName);
            sectionBean.setSectionId(sectionId);
            sectionBean.setMore(true);
            beanList.add(sectionBean);

            final JSONArray goods = jsonObject.getJSONArray("goods");
            final int goodsSize = goods.size();
            for (int j = 0; j < goodsSize; j++) {
                final JSONObject contentItem = goods.getJSONObject(j);
                final int goodsId = contentItem.getInteger("goods_id");
                final String goodsThumb = contentItem.getString("goods_thumb");
                final String goodsName = contentItem.getString("goods_name");

                SectionItemEntity itemEntity = new SectionItemEntity(goodsId, goodsName, goodsThumb);
                beanList.add(new SectionBean(itemEntity));
            }
        }
        return beanList;
    }
}
